package cl.android.mochileros;

import java.util.List;

import model.Categoria;
import model.Lugar;
import model.LugarDAO;
import android.app.Fragment;
import android.app.FragmentManager;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

public class LugaresFragment extends Fragment{

	private ListView listView;
//	private String [] lugares;
////	private Bitmap imagen;
	LugarDAO lugarDao;
	
	Categoria categoria;

	public LugaresFragment(Categoria categoria) {
		this.categoria = categoria;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View inflatedView =  inflater.inflate(R.layout.lugares_fragment_layout, container,false);
		return inflatedView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		lugarDao  = new LugarDAO(getActivity());
		Lugar lugarExample = new Lugar();
		lugarExample.setIdCategoria(categoria.getIdCatergoria());
		List<Lugar> lugares = lugarDao.buscarPorExample(lugarExample);
		listView = (ListView) getActivity().findViewById(R.id.lugares_list);
		
//		listView.setAdapter(new ArrayAdapter<Lugar>(getActivity(), 
//				R.layout.lugares_list_item,R.id.nombre_lugar,lugares));
		listView.setAdapter(new LugaresAdapter(getActivity(), lugares));
		int[] colors = {0, 0xFFFF0000, 0}; // red for the example
		listView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
		listView.setDividerHeight(1);
		listView.setOnItemClickListener(new ListItemClickListener());
	}
	 /* The click listner for ListView in the navigation drawer */
    private class ListItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }
    
    public void selectItem(int position){
		
    	Lugar lugar = (Lugar) listView.getItemAtPosition(position);
//    	listView.setItemChecked(position, true);
    	abrirFragmentLugar(lugar);
    	
		
//		setTitle(sections[position]);
//		drawerLayout.closeDrawer(listView);
	}
    
    public void abrirFragmentLugar(Lugar lugar){
    	cargarFragment(new LugarFragment(lugar));
    }
    public void cargarFragment(Fragment fragment){
//    	getFragmentManager().beginTransaction()
//        .add(fragment, "fragmento")
//        .addToBackStack(null)
//        .commit();
		FragmentManager manager = getFragmentManager();
		manager.beginTransaction().add(R.id.mainContent, fragment, "fragmento").addToBackStack(null).commit();
	}
    
}
